<?php
include_once dirname(__FILE__).'/Calendar.class.php';
include_once dirname(__FILE__).'/Day.class.php';

class Month {
	static private $FIRST_DAY = 1;
	static private $DAY_IN_WEEK = 7;
	static private $MONTH_IN_YEAR = 12;
	
	private $_changed = true;
	private $_month;
	private $_year;
	
	private $_prev_month;
	private $_prev_year;
	private $_next_month;
	private $_next_year;
	
	private $_month_array;
	
	function __construct($month, $year) {
		$this->setMonth($month, $year);
	}
	
	public function setMonth($month, $year) {
		$this->_month = ($month == null)?date("n"):$month;
		$this->setYear($year);
		$this->_changed = true;
	}
	
	public function setYear($year) {
		$this->_year = ($year == null)?date("Y"):$year;
		$this->setParams();
	}
	
	public function getPrevMonth() { return $this->_prev_month; }
	public function getPrevYear() { return $this->_prev_year; }
	public function getNextMonth() { return $this->_next_month; }
	public function getNextYear() { return $this->_next_year; }
	
	public function getValue() { return $this->_month; }
	
	private function setParams() {
		$this->_prev_month = $this->_month - 1;
		$this->_prev_year = $this->_year;
		if ($this->_prev_month < 1) {
			$this->_prev_month = self::$MONTH_IN_YEAR;
			$this->_prev_year -= 1;
		}
		
		$this->_next_month = $this->_month + 1;
		$this->_next_year = $this->_year;
		if ($this->_next_month >= self::$MONTH_IN_YEAR) {
			$this->_next_month = 1;
			$this->_next_year += 1;
		}
	}
	
	/**
	 * 
	 * @return array
	 */
	public function generate() {
		if ($this->_changed) {
			$ItemDay = 0;
			$ItemD = 0;
			$this->_month_array = array();
			
			$day = "$this->_year-$this->_month-".self::$FIRST_DAY;
			
			$prev_month_last_day = Calendar::getDate("t", "$this->_prev_year-$this->_prev_month-".self::$FIRST_DAY);
			
			$first_week = Calendar::getDate("W", $day);
			$last_day = Calendar::getDate("t", $day);
			$tail_month = Calendar::getDate("w", $day);
			$tail_month = (!$tail_month)?5:$tail_month -= 2;
			
			$week_num = $first_week - 1;
			
			while ($ItemDay < $last_day){
				if ($ItemD % self::$DAY_IN_WEEK == 0) {
					$SevenDay = 1;
					++$week_num;
					while ($tail_month >= 0) {
						$this->_month_array[$week_num][$SevenDay] = new Day($prev_month_last_day - $tail_month, $this->_prev_month, $this->_prev_year, $week_num, $SevenDay);
						++$SevenDay;
						--$tail_month;
						++$ItemD;
					}
				}
				$this->_month_array[$week_num][$SevenDay] = new Day($ItemDay + 1, $this->_month, $this->_year, $week_num, $SevenDay);
				++$SevenDay;
				++$ItemD;
				++$ItemDay;
			}
			
			$next_m = $SevenDay;
			for ($j = 1; $j <= self::$DAY_IN_WEEK +1 - $next_m; ++$j){
					$this->_month_array[$week_num][$SevenDay] = new Day($j, $this->_next_month, $this->_next_year, $week_num, $SevenDay);	
					++$SevenDay;
			}
			
			$this->_changed = false;
		}
		
		return $this->_month_array;
	}
}


?>